<template>
  <div style="width: 100%; height: 100%;">
    <div style="width: 100%;height:25vh;">
      <h3>请确认收货人信息</h3>
      <el-table :data="data.orditem" style="width: 100%">
        <el-table-column prop="order.ship2name" label="收货人" width="180" />
        <el-table-column prop="order.ship2phone" label="收货人电话号" width="180" />
        <el-table-column prop="order.ship2addr" label="收货地址" />
      </el-table>
    </div>
    <div>
      <el-table :data="data.orditem" style="width: 100%">
        <el-table-column property="goods.bookname" label="商品名称" width="120" align="center"></el-table-column>
        <el-table-column label="商品图片" align="center">
          <template #default="scope">
            <div class="image-container">
              <img :src="`/api/public/findgoodsimg/${scope.row.goods.gid}`" alt="商品图片" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="num" label="数量" width="180" />
        <el-table-column prop="price" label="商品单价" width="180" />
        <el-table-column prop="" label="" />
      </el-table>
    </div>
    <el-row>
      <el-col :span="8">
        <el-countdown 
          title="请您在3分钟内完成支付，否则订单会被自动取消" 
          :value="value" 
          @finish="countdownFinish"
        />
      </el-col>
    </el-row>
    <div>
      <div>
        <el-form
          :label-position="labelPosition"
          label-width="auto"
          :model="formLabelAlign"
          style="max-width: 600px"
        >
          <el-form-item label="付款方式:" label-position="right">
            <el-radio-group v-model="data.tot" aria-label="label position">
              <el-radio-button value="0">余额宝</el-radio-button>
              <el-radio-button value="1">支付宝</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div>
      <el-table :data="data.orditem" style="width: 100%">
        <el-table-column prop="order.amount" label="应付金额" />
      </el-table>
      <el-button @click="add()">提交表单</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, onMounted, ref } from 'vue';
import { $get, $postx, $postf } from "../../ajax/index.js";
import myalert from "../../utils/myalert.js";
import alert from "../../components/Alert.vue";
import store from "../../store/index.js";
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'; // 确保导入ElMessage
import type { FormItemProps, FormProps } from 'element-plus'
import dayjs from 'dayjs'
import { Calendar } from '@element-plus/icons-vue'

// 定义响应式数据
const data = reactive({
  user:[],
  shippingAddress: '',
  orders:[],
  orditem:[],//订单明细
  goods: [],
  tot:0,//0是用余额宝，1是支付宝
  totalPrice: 0
});

const value = ref(Date.now() + 1000 * 60 * 3) // 3分钟倒计时
const labelPosition = ref<FormProps['labelPosition']>('right')
const router = useRouter(); // 获取路由实例

// 倒计时结束处理函数
const countdownFinish = () => {
  ElMessage.warning('支付超时，订单已取消');
  router.push('/login'); // 跳转到订单取消页面
}

const route = useRoute();
onMounted(() => {
  data.user.uname=store.state.user.uname;
  data.user.addr=store.state.user.addr;
  data.user.phone=store.state.user.phone;
  
  $get("/api/seller/findOrder")
  .then((resp)=>{
    console.log(resp.data.data);
    data.orditem=resp.data.data;
  })
});

function add(){
  console.log(data.tot)
  console.log(data.orditem)
  
  // 准备要提交的数据
  const paymentData = {
    orderItems: data.orditem,
    paymentMethod: data.tot
  }
  
  $postf("/api/seller/payment/", paymentData)
    .then((resp) => {
      console.log(resp);
      ElMessage({
        message: "付款成功!!!",
        type: 'success',
      });
      router.push('/order/success'); // 跳转到支付成功页面
    })
    .catch((error) => {
      console.error(error);
      ElMessage.error('付款失败，请重试');
    });
}
</script>

<style scoped>
h2 {
  margin-bottom: 10px;
}
ul {
  list-style: none;
  padding: 0;
}
li {
  margin-bottom: 5px;
}
.image-container {
  width: 80px;
  height: 80px;
  overflow: hidden;
}
.image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>